<template>
    <section class="content">
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
              <el-form-item>时间：
                <el-date-picker v-model="time" type="date"
              placeholder="选择日期" value-format="yyyy-MM-dd" ></el-date-picker>
                数据类型：
                <el-select v-model="typeid" clearable placeholder="全部">
                  <el-option v-for="item in type" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <el-form :inline="true">
                测点：
              <el-tag
                  :key="tag"
                  v-for="tag in dynamicTags"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag)">
                  {{tag}}
                </el-tag>
               <el-select 
                 v-if="inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm"
                  @change="handleInputConfirm"
                  clearable
                  placeholder="请选择" 
                  >
                  <el-option v-for="item in queryPoint" :key="item.id" :label="item.name" :value="item.name"></el-option>
                </el-select>
                 <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 测点</el-button>
              
              <el-form-item>

                <el-button type="primary" @click="inquire">查询</el-button>
              </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="14" class="bc-w" >
            <div class="chartLine" id="chartLine"></div>
        </el-col>
    </section>
</template>
<script >
import echarts from "echarts";
export default {
  data() {
    return {
      time:'',
      projectId:'',
      queryPoint:[],
      chartLine: null,
      type:[
        {id:1,name:"设备数据"},
        {id:2,name:"人工数据"}
      ],
      typeid:"",
      value2:[],
      series:[],
      xAxis:[],
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      chert:[],
      symbolSize:5,
    };
  },
  methods: {
    timefirst(t){
      this.value2 = t
    },
    selectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.queryPoint.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
    handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
        this.inquire();
      },
     showInput() {
        this.inputVisible = true;
      },
    handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
          this.dynamicTags = this.dynamicTags
          this.inquire();
        }
        this.inputVisible = false;
        this.inputValue = '';
    },
     inquire(){
          var projectId = this.projectId
          var dynamicTags = this.dynamicTags
          var time = this.time

          this.projectModel.settleDatapoints(projectId, dynamicTags, time).then(res => {
           if(res.status != 200) {
             this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
          }
      
          this.series = res.data.series;
          this.chert = res.data
          this.drawCharts();
        }).catch(err => {
          console.log(err.message)
        })
    },
    drawLineChart() {
      this.chartLine = echarts.init(document.getElementById("chartLine"));
      var _this = this
      var series = [];
      var series1 = [];
      for (var i = 0; i <  _this.series.length; i++) {
        series.push({
          name:_this.series[i].name,
          type: "line",
          smooth: false,
          symbolSize: _this.symbolSize,
          data: _this.series[i].data,
        });
        series1.push({
          name:_this.series[i].name,
        })
      }
      this.chartLine.setOption({
        title: {
          left: "center",
          text: this.time+"测点对比",
        },
        legend: {
              top: 30,
              data: series1
            },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        toolbox: {
            orient: 'vertical',
            show: true,
            itemSize: 10,
            feature: {
              dataZoom: { yAxisIndex: "none" },
              restore: {},
              dataView: { readOnly: false }, //数据视图
              saveAsImage: {}
            }
          },
        xAxis: [
          {
          type: 'value',
          axisLine: {onZero: false},
          axisLabel:{formatter:'{value}(mm)'}
          }
        ],
        yAxis: [
          {
          type: 'value',
          axisLine: {onZero: false},
          axisLabel:{formatter:'{value}(m)'},
          }
        ],
        series: series
      });
    },
    drawCharts() {
      this.drawLineChart();
    }
  },
  mounted() {
    this.projectId = this.$route.query.projectId;

    let data = {
      projectId: this.projectId,
      monitorItemId: "13",
      resultType: "3"
    };
    this.projectModel
      .queryPoint(data)
      .then(res => {
        this.queryPoint = res.data;
        if(this.queryPoint.length > 0) {
            this.dynamicTags[0] = this.queryPoint[0].name;
            this.time =  moment(this.queryPoint[0].newData.date,'YYYY-MM-DD HH:mm:ss').format('YYYY-MM-DD');
            this.inquire();
        }
      })
      .catch(err => {
        console.log(err.message);
      });
  }
};
</script>


<style scoped>
.content {
  padding-top: 20px;
  background-color: #ffffff;
  min-height: 680px;
}
.bc-w {
  width: 100%;
  height: calc(100% - 90px);
  padding: 10px;
}
.chart-plane {
  height: 100%;
}
.chartLine {
  width: calc(40% - 20px);
  height: calc(100% - 20px);
  padding: 10px 30%;
}
.select-w-100 {
  width: 100px;
}
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

</style>
